<template>
  <div class='container'>
  <div class="passwordsSetting">
    <el-form :model="ruleForm2" :rules="rules2" class="demo-ruleForm" label-width="100px" ref="ruleForm2" status-icon="">
      <el-form-item label="原密码" prop="oldPasswrod">
        <el-input v-model="ruleForm2.oldPasswrod" type="password">
        </el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input auto-complete="off" type="password" v-model="ruleForm2.newPassword">
        </el-input>
      </el-form-item>

      <el-form-item class="submit">
        <el-button @click="submitForm('ruleForm2')" type="primary">
          确认修改
        </el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>
<script>
import text from "@/assets/images/text.png";
import {userPasswordEdit,} from '@/pages/api/index.js'
  export default {
    data() {
      var pass1 = (rule, value, callback) => {
        if (!value) {
          return callback(new Error("原密码不能为空"));
        }
        callback();
      };
      var pass = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入密码"));
        } else {

          callback();
        }
      };
      return {
        ruleForm2: {
          newPassword: "",
          oldPasswrod: "",
        },
        rules2: {
          newPassword: [{ validator: pass, trigger: "blur" }],
          oldPasswrod: [{ validator: pass1, trigger: "blur" }],
        }
      };
    },
    methods: {
      submitForm(formName) {
        let _this = this;
        _this.$refs[formName].validate(valid => {
          if (valid) {
            const content = {
              oldPassword:this.ruleForm2.oldPasswrod,
              newPassword:this.ruleForm2.newPassword
            }
            userPasswordEdit(content).then(res => {
              if(res.code == 0)  {
                this.$message.success('修改成功');
                this.$router.push({
                  path:'/'
                })
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted(){
      if(this.$route.path === '/work/PasswordSetting') {
        this.$emit('changeWorkIndex', {title:'修改登陆密码',index:'6-1'})
      }
    }
  };
</script>
<style lang="scss">
  .container{
      width:100%;
      height:100%;
      background-color: #fff;
        .passwordsSetting {
          width: 25%;
          margin: 0 auto;
          padding-top:100px;
          /*border:1px solid #000;*/
          .submit {
            text-align: center;
          }
  }
  }
</style>
